package com.china08.yxyapi.api.manage.edu.platform.resource.yxylectureprepack;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack;
import com.china08.yxyapi.entity.mg.lecture.YxyLecturePrePack.Res;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.lecture.YxyLecturePrePackRepository;
import com.china08.yxyapi.util.DateToolUtils;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLecturePrePackResourceController extends YxyApiBaseController {

    Logger log = Logger.getLogger(YxyLecturePrePackStatusController.class);

    @Autowired
    YxyLecturePrePackRepository yxyLecturePrePackRepository;

    @ApiOperation(value = "备课包资源详情")
    @RequestMapping(value = "/api_yw/manage/edu/platform/resource/lectureprepack/resource", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request,
            HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String prepackId = reqModel.prepackId;
        YxyLecturePrePack yxyLecturePrePack = yxyLecturePrePackRepository
                .findById(prepackId);
        Assert.isTrue(null != yxyLecturePrePack, "备课包为空");

        RespModel model = new RespModel();

        model.packName = yxyLecturePrePack.getPackName();
        model.userNick = yxyLecturePrePack.getUserNick();
        model.schoolName = yxyLecturePrePack.getSchoolNick();
        model.createdDate = DateToolUtils
                .format(yxyLecturePrePack.getCreatedDate(), "yyyy-MM-dd");
        model.reslist = yxyLecturePrePack.getResources();

        return model;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String prepackId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(prepackId), "备课包为空");
        }
    }

    protected class ResModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        // 包名
        public String packName;
        // 资源数量
        public Integer resCount;
        // 贡献者
        public String userNick;
        // 学校
        public String schoolName;
        // 创建日期
        public String createdDate;
        // 资源
        public List<Res> reslist;
    }
}
